package com.ljc.cjgl.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ljc.cjgl.custom.FileUtil;
import com.ljc.cjgl.custom.TablePageable;
import com.ljc.cjgl.custom.TableResult;
import com.ljc.cjgl.entitiy.Stu;
import com.ljc.cjgl.entitiy.TbClass;
import com.ljc.cjgl.form.StudentForm;
import com.ljc.cjgl.security.UserUtils;
import com.ljc.cjgl.service.ClassService;
import com.ljc.cjgl.service.StuService;

@Controller
@RequestMapping(value="/stu")
public class StuContorller {
	@Autowired
	private StuService service;
	@Autowired
	private ClassService classService;
	@Autowired
	private UserUtils userUtils;
	
	private Specification<Stu> buildSpec(StudentForm form){
		HashSet<Rule> rules=new HashSet<>();
		String name = form.getName();
		if(StringUtils.hasText(name)) {
			rules.add(Rule.property("name").contain(name));
		}
		String bjMc = form.getBjMc();
		if(StringUtils.hasText(bjMc)) {
			rules.add(Rule.property("tbClass.name").contain(bjMc));
		}
		String sex = form.getSex();
		if(StringUtils.hasText(sex)) {
			rules.add(Rule.property("sex").equal(sex));
		}
		String search = form.getSearch();
		if(StringUtils.hasText(search)) {
			HashSet<Rule> rules2=new HashSet<>(); 
			rules2.add(Rule.property("name").contain(search));
			rules2.add(Rule.property("idCard").contain(search));
			rules2.add(Rule.property("job").contain(search));
			rules2.add(Rule.property("sex").contain(search));
			rules2.add(Rule.property("tbClass.name").contain(search));
			rules2.add(Rule.property("username").contain(search));
			rules2.add(Rule.property("address").contain(search));
			rules2.add(Rule.property("phone").contain(search));
			rules.add(Rule.or(rules2));
		}
		return Rule.and(rules).toSpecification();
	}
	
	@RequestMapping(value="/manage")
	public String manage(ModelMap map) {
		List<TbClass> classes = classService.findAll();
		map.put("classes",classes);
		return "stu/manage";
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public Object page(TablePageable pageable,StudentForm form) {
		PageRequest pageRequest = pageable.bulidPageRequest();
		Page<Stu> page = service.findAll(buildSpec(form), pageRequest);
		return TableResult.bulidPageResult(page);
	}
	
	@RequestMapping(value="/edit")
	public String edit(Integer id , String bjMc,ModelMap map) {
		Stu model=new Stu();
		if(id!=null) {
			model=service.findById(id);
		}
		List<TbClass> bjs=classService.findAll();
		if(StringUtils.hasText(bjMc)) {
			bjs=classService.findNameLike(bjMc);
		}
		map.put("bjs",bjs);
		map.put("model",model);
		return "stu/edit";
	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(MultipartFile file,StudentForm form,HttpServletRequest request) throws IOException {
		Integer id = form.getId();
		if(id!=null) {
			if(userUtils.hasRole("ROLE_STUDENT")&&id!=userUtils.getUser().getId()) {
				return TableResult.bulidResult(false, "不能修改别人的数据");
			}
		}
		if(service.save(file,form)) {
			return TableResult.bulidResult(true, "数据保存成功");
		}else {
			return TableResult.bulidResult(false, "数据保存失败");
		}
	}
	
	@RequestMapping(value="/download")
	@ResponseBody
	public Object download(HttpServletRequest request,HttpServletResponse response,Integer id,String uid) {
		Stu student=null;
		if(id!=null) {
			student = service.findById(id);
		}else {
			student=service.findByUid(uid);
		}
		FileUtil.download(request,response,student.getSaveName(),student.getPhotoName());
		return null;
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	@PreAuthorize("hasRole('ROLE_ADMIN')||hasRole('ROLE_DEVELOPER')")
	public Object delete(@RequestBody List<Stu> students) {
		HashMap<String, Object> result=new HashMap<>(); 
		if(service.delete(students)) {
			TableResult.bulidResult(true, "数据删除成功");
		}else {
			TableResult.bulidResult(false, "数据删除失败");
		}
		return result;
	}
	
	@RequestMapping(value="/passwordReset")
	@ResponseBody
	@PreAuthorize("hasRole('ROLE_ADMIN')||hasRole('ROLE_DEVELOPER')")
	public Object arrselectedData(Integer id) {
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
		Stu stu=service.findById(id);
		stu.setPassword(encoder.encode("111111"));
		service.save(stu);
		return "密码复位成功";
	}
}
